package com.zxbg.dao;

import com.zxbg.domain.Process;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface ProcessDao {
    @Insert("insert into process values(null, #{process_rule_current_node}, #{process_rule_code}, #{submit_employee_id}, #{current_task_handler}, #{process_status}, NOW(), #{tenant_id}, #{app_id}, #{all_handler}, #{form_data_id}, 0)")
    public int addNewProcess(Process process);

    @Delete("delete from process where process_id = #{process_id}")
    public int deleteProcessById(Integer process_id);

    @Update("update process set submit_time = NOW() where process_id = #{process_id}")
    public int updateDraftProcess(Integer process_id);

    @Insert("insert into process values(null, null, null, #{submit_employee_id}, null, null, NOW(), #{tenant_id}, #{app_id}, null, #{form_data_id}, 1)")
    public int addDraftProcess(Process process);

    @Select("select * from process where process_id = #{process_id}")
    public Process getProcessById(Integer process_id);

    @Select("select * from process where submit_employee_id = #{submit_employee_id} and if_draft = 1 and tenant_id = #{tenant_id}")
    public List<Process> getDraftProcess(@Param("submit_employee_id") String applicant_id, @Param("tenant_id") Integer tenant_id);

    @Select("select * from process where submit_employee_id = #{submit_employee_id} and if_draft = 0 and tenant_id = #{tenant_id}")
    public List<Process> getAllMyApply(@Param("submit_employee_id") String applicant_id, @Param("tenant_id") Integer tenant_id);

    @Select("select * from process where submit_employee_id = #{submit_employee_id} and if_draft = 0 and tenant_id = #{tenant_id} and process_status = #{process_status}")
    public List<Process> getTargetTypeMyApply(@Param("process_status") String process_status, @Param("submit_employee_id") String applicant_id, @Param("tenant_id") Integer tenant_id);

    @Select("select * from process where submit_employee_id = #{submit_employee_id} and if_draft = 0 and tenant_id = #{tenant_id} and app_id = #{app_id}")
    public List<Process> getTargetAppMyApply(@Param("app_id") String app_id, @Param("submit_employee_id") String applicant_id, @Param("tenant_id") Integer tenant_id);

    @Select("select * from process where submit_employee_id = #{submit_employee_id} and if_draft = 0 and tenant_id = #{tenant_id} and process_status = #{process_status} and app_id = #{app_id}")
    public List<Process> getAppTypeMyApply(@Param("process_status") String process_status, @Param("app_id") String app_id, @Param("submit_employee_id") String applicant_id, @Param("tenant_id") Integer tenant_id);

    @Select("select * from process where (current_task_handler like '${current_task_handler},%' or current_task_handler like '%,${current_task_handler},%' or current_task_handler like '%,${current_task_handler}' or current_task_handler = #{current_task_handler}) and tenant_id = #{tenant_id} and process_status = 'inApproval'")
    public List<Process> getAllMyTodo(@Param("current_task_handler") String current_task_handler, @Param("tenant_id") Integer tenant_id);

    @Select("select * from process where (current_task_handler like '${current_task_handler},%' or current_task_handler like '%,${current_task_handler},%' or current_task_handler like '%,${current_task_handler}' or current_task_handler = #{current_task_handler}) and tenant_id = #{tenant_id} and app_id = #{app_id} and process_status = 'inApproval'")
    public List<Process> getAppTypeMyTodo(@Param("app_id") String app_id, @Param("current_task_handler") String current_task_handler, @Param("tenant_id") Integer tenant_id);

    @Select("select process_id from process order by process_id desc limit 1")
    public int getNewestProcessId();

    @Update("update process set process_rule_current_node = null, current_task_handler = null, process_status = 'end', all_handler = null where process_id = #{process_id}")
    public int setProcessEnd(Integer process_id);

    @Update("update process set process_rule_current_node = #{process_rule_current_node}, current_task_handler = #{current_task_handler}, all_handler = #{all_handler} where process_id = #{process_id}")
    public int updateInApprovalProcess(Process process);

    @Update("update process set current_task_handler = #{current_task_handler} where process_id = #{process_id}")
    public int setProcessNextOrderNode(@Param("process_id") Integer process_id, @Param("current_task_handler") String current_task_handler);

    @Update("update process set process_status = 'end' where process_id = #{process_id}")
    public int setProcessReject(Integer process_id);

    @Select("select * from process where (all_handler like '${all_handler},%' or all_handler like '%,${all_handler},%' or all_handler like '%,${all_handler}' or all_handler = #{all_handler}) and current_task_handler <> #{all_handler} and tenant_id = #{tenant_id} and process_status = 'inApproval'")
    public List<Process> getAllWillTodo(@Param("all_handler") String all_handler, @Param("tenant_id") Integer tenant_id);

    @Select("select * from process where (all_handler like '${all_handler},%' or all_handler like '%,${all_handler},%' or all_handler like '%,${all_handler}' or all_handler = #{all_handler}) and current_task_handler <> #{all_handler} and app_id = #{app_id} tenant_id = #{tenant_id} and process_status = 'inApproval'")
    public List<Process> getAppTypeMyWillDo(@Param("app_id") String app_id, @Param("all_handler") String all_handler, @Param("tenant_id") Integer tenant_id);

    @Select("select * from process where tenant_id = #{tenant_id} and if_draft = 0")
    public List<Process> getAllProcess(Integer tenant_id);

    @Update("update process set process_status = 'stop' where process_id = #{process_id}")
    public int setProcessStop(Process process);

    @Update("update process set current_task_handler = #{current_task_handler} where process_id = #{process_id}")
    public int setCurrentTaskHandlerByProcessId(@Param("current_task_handler") String current_task_handler, @Param("process_id") Integer process_id);

    @Update("update process set process_rule_current_node = #{process_rule_current_node}, current_task_handler = #{current_task_handler}, all_handler = #{all_handler} where process_id = #{process_id}")
    public int setProcessBack(Process process);
}
